import 'package:flutter/services.dart';
import 'package:flutter_demo/ui/pages/read/tools/book_controller.dart';
import 'package:get/get.dart';

import 'index.dart';

class ReadController extends GetxController {
  ReadController();

  final state = ReadState();

  //单行文本高度
  double textHeight = 10;

  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.bookController = BookController();
    loadData();
  }

  void loadData() {
    state.isOver = false;
    textHeight = state.calculateTextHeight('疯', state.fontSize);
    rootBundle.loadString('assets/santi.txt').then((value) {
      /// 文本区域高度 \r\n 计算有误 ... 高度有误
      state.data = value.replaceAll('\r\n', '\n');

      /// 文本最大高度 648  运算符 ~/：整除
      state.maxTextHeight =
          ((state.maxHeight - state.padding.vertical) ~/ textHeight) *
              textHeight;
      state.loadPages().then((value) {
        state.isOver = true;
        //通知view.dart刷新页面
        update();
        state.bookController.goTo(1);
      });
    });
  }

  //修改文字大小
  void updateFontSize(
    double fontSize,
  ) {
    /// 改变字号
    state.fontSize = fontSize;
    textHeight = state.calculateTextHeight('疯', fontSize);

    /// 文本最大高度 648
    state.maxTextHeight = (state.maxHeight ~/ textHeight) * textHeight;
    state.isOver = false;
    state.loadPages().then(
      (value) {
        state.isOver = true;
        update();
      },
    );
  }
}
